package com.example.wan1.view.project;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.text.Html;
import android.view.View;

import com.example.wan1.R;
import com.example.wan1.base.adapter.SubPagerAdapter;
import com.example.wan1.base.fragment.SimpleFragment;
import com.example.wan1.bus.RxBus;
import com.example.wan1.constant.Constants;
import com.example.wan1.model.ArticleBean;
import com.example.wan1.model.ProjectCategoryBean;
import com.example.wan1.model.common.CommonResList;
import com.example.wan1.utils.ToolsUtils;
import com.example.wan1.view.home.PerProjectFragment;
import com.google.android.material.tabs.TabLayout;

import java.util.List;

import javax.inject.Inject;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;

/***
 * @Author: zxd
 * @CreateDate: 2020/6/29 10:42
 * @Description: 项目，作为第二个tab
 * 需要请求网络活得项目分类
 */
public class ProjectsFragment extends SimpleFragment implements ProjectContract.View {

    @BindView(R.id.project_tab)
    TabLayout mProjectTab;
    @BindView(R.id.project_vp)
    ViewPager mProjectVp;

    @Inject
    List<String> mTitles;
    @Inject
    List<Fragment> mFragments;
    @Inject
    SubPagerAdapter mAdapter;
    @Inject
    ProjectPresenter mPresenter;
    @BindView(R.id.fake_status_bar)
    View mFakeStatusBar;

    @Override
    protected void initData() {
        //mList = new ArrayList<>();
        //mTitles = new ArrayList<>();
        //mFragments = new ArrayList<>();

        showLoading();
        mPresenter.getProjectCategory();
    }

    @Override
    protected void initView() {

        //mAdapter = new SubPagerAdapter(getChildFragmentManager(),
        // FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
        mProjectTab.setupWithViewPager(mProjectVp);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mProjectTab.setElevation(0);
        }

    }

    @Override
    protected int initLayoutRes() {
        return R.layout.fragment_project;
    }

    @Override
    public void onSuccess(CommonResList<ArticleBean> data) {

    }

    @Override
    public void onFailure(String msg) {
        showError();
    }

    @Override
    public void onCategoryProject(List<ProjectCategoryBean> data) {
        hideLoading();
        //mList.clear();
        mTitles.clear();
        mFragments.clear();

        //mList.addAll(data);
        for (int i = 0; i < data.size(); i++) {
            mTitles.add(Html.fromHtml(data.get(i).getName()).toString());
            mFragments.add(new PerProjectFragment().newInstance(Constants.PROJECT_NORMAL_TYPE, data.get(i).getId()));
        }
        mAdapter.setData(mFragments, mTitles);
        mProjectVp.setAdapter(mAdapter);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        RxBus.getDefault().subscribe(mContext, new RxBus.Callback<String>() {
            @Override
            public void onEvent(String s) {
                if (s.equals("theme_color")) {
                    int color = ToolsUtils.getThemeColor(getActivity());
                    mProjectTab.setSelectedTabIndicatorColor(color);
//                    mProjectTab.setTabTextColors(ColorStateList.valueOf(color));
                    mProjectTab.setTabTextColors(Color.parseColor("#333333"), color);
                }
            }
        });
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        int color = ToolsUtils.getThemeColor(getActivity());
        if (color != 0) {
            mProjectTab.setSelectedTabIndicatorColor(color);
            mProjectTab.setTabTextColors(Color.parseColor("#333333"), color);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        RxBus.getDefault().unregister(mContext);
    }

    public void setTvTitleBackgroundColor(int color) {
        mFakeStatusBar.setBackgroundColor(color);
    }
}
